var currentDepId;
var bookList;
var currentDepName;
var checkId;
$(document).ready(function () {
    var depList = $('#depList');
    bookList = $('#bookList');
    $('.xh-datagrid-toolbar').parseToolbar();
    bookList.initDataGrid(moduleUrl + 'staff/getStaffData/true').setSelectThenEnable(['bt_list_manageGroup'],['bt_list_auth']);

    depList.treegrid({
        selectOnCheck:false,
        checkOnSelect:false,
        onSelect:function (row) {
            currentDepId=row['iDepId'];
            currentDepName=row['sDepName'];
            depList.treegrid('expand',currentDepId);
            bookList.addDataGridParams({depId:currentDepId}).datagrid('getPanel').panel('setTitle', '通讯录（' + row['sDepName'] + '）');
        },
        // onBeforeSelect:function (row) {
        //     if(currentDepId==undefined){
        //         return true;
        //     }else {
        //         if(currentDepId==row['iDepId']){
        //             depList.treegrid('unselect',currentDepId);
        //             depList.treegrid('collapse',currentDepId);
        //             currentDepId=null;
        //             return false;
        //         }
        //     }
        // },
        // onBeforeCheck:function (row) {
        //     if(currentDepId==undefined){
        //         return true;
        //     }else {
        //         if(currentDepId==row['iDepId']){
        //             depList.treegrid('uncheckRow',currentDepId);
        //             currentDepId=null;
        //             return false;
        //         }
        //     }
        // },
        onBeforeCheck:function (row) {
            if(checkId==undefined || checkId==null){
                checkId = row['iDepId'];
                return true;
            }else {
                if(checkId==row['iDepId']){
                    depList.treegrid('uncheckRow',row['iDepId']);
                    checkId =null;
                    return false;
                }else {
                    depList.treegrid('uncheckRow',checkId);
                }
            }
        },
        onCheck:function (row) {
            checkId = row['iDepId'];
        },
        onLoadSuccess:function (row, data) {
            depList.treegrid('select',data[0]['iDepId']);
        }

    });

    DataGrid.onLoadSuccess = function(data){
        bookList.dataGridClear();
    };


    //管理组别
    $('#bt_list_manageGroup').click(function () {
        // var iUserIds = bookList.getDataGridSelectedFields('iUserId');
        // window.top.xhTop.showModal({
        //     title: '管理组',
        //     content: '<iframe src="'+baseUrl+'hrm/staff/manageGroup?iUserIds='+iUserIds+'"  style="width: 100%;;border-style: none;padding: 0px;height: 120px"  scrolling="yes"   ></iframe>',
        //     button: '提交'
        // })

        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var $depArray=depList.treegrid('getChecked');
        if($depArray.length>0){
            var depId=$depArray[0]['iDepId'];
            var depName=$depArray[0]['sDepName'];
            var staffIds=bookList.getDataGridSelectedFields('iStaffId');
            var staffName=bookList.getDataGridSelectedFields('sStaffName');
            window.top.swal({
                title: '添加人员到部门',
                text: '你确定要将'+staffName+'移动到'+depName+"部门",
                type: 'warning',
                showCancelButton: true,
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(function() {
                $.ajax({
                    url:moduleUrl+'staff/moveGroup',
                    data:{depId:depId,staffIds:staffIds},
                    type:'post',
                    success:function (data) {
                        depList.treegrid('select',depId);
                        // bookList.addDataGridParams({depId:depId}).datagrid('getPanel').panel('setTitle', '通讯录（' + row['sDepName'] + '）');
                        bookList.dataGridClear();
                        window.top.swal(
                            '添加成功',
                            ''+staffName+'已成功添加到'+depName+"部门",
                            'success'
                        )
                    },
                    error:function (data) {
                        window.top.swal(
                            '失败',
                            data['responseText'],
                            'error'
                        );
                    }

                })

            }, function(dismiss) {
                // dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
                if (dismiss === 'cancel') {
                    window.top.swal(
                        '取消',
                        '取消添加',
                        'error'
                    )
                }
            });
        }else {
            window.top.swal(
                '',
                '请先选择部门',
                'error'
            )
        }
    });

    xhTool.bindDocKeyDown($('#bt_list_search'));
    $('#bt_list_search').click(function () {
        var key=xhExtra.getValue($('#list_search'));
        bookList.addDataGridParams({key:key,depId:currentDepId});
    });

    // User/extraSchoolList?iUserId=8782
    $('#bt_list_auth').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }

        var id = bookList.getDataGridSelectedField('iUserId');
        if(nowUserId !=id){
            window.top.xhTop.showModal({
                title: '设置权限',
                content: '<iframe src="'+baseUrl+'power/extra?type=school&target=main&goal=1&goalId='+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 500px"  scrolling="yes"   ></iframe>',
                size: 'lg',
                button: '提交',
            })
        }else {
            window.top.swal('错误','不能给自己分配权限','error');
        }

    });
});